home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt86aug.arc / TYPES.DOC < prev   
Text File  |  1980-01-01  |  11KB  |  319 lines

  1. Screen # 1
  2.   0 ( Type definitions                                    
  3.   1
  4.   2 ( Working variables for object compiler)
  5.   3 VARIABLE SIZE        ( Holds storage size of type)
  6.   4 VARIABLE INHERIT     ( Holds address of inherited 
  7.                            ops vocab)
  8.   5 VARIABLE OPS         ( Holds address of end of 
  9.                            ops vocabulary)
  10.   6 VARIABLE STASH       ( Temporary store for current 
  11.                            vocabulary)
  12.   7 VARIABLE PUBLIC      ( Holds link to ordinary 
  13.                            dictionary)
  14.   8 VARIABLE LASTLOCAL   ( Holds address of last word 
  15.                            in type)
  16.   9 VARIABLE IN.TYPE.DEF?  ( Flag; are we in a type 
  17.                              definition?)
  18.  10 VARIABLE VAL         ( Flag; has a VAL index 
  19.                            been declared?)
  20.  11
  21.  12 0 CONSTANT FALSE   FALSE NOT   CONSTANT TRUE  
  22.     ( For readability)
  23.  13
  24.  14 ( I refuse to use THEN, which is a syntactic 
  25.       abomination!!)
  26.  15 : ENDIF  [COMPILE] THEN ;  IMMEDIATE      -->
  27.  
  28. Screen # 2
  29.   0 ( Type definitions                                   
  30.   1
  31.   2 ( Make a third stack to hold current object's 
  32.       address ; its size
  33.   3   determines how deeply type definitions 
  34.       may be compounded)
  35.   4  CREATE OSTACK  HERE 16 + ,  16 ALLOT                                       
  36.   5
  37.   6 ( Push parameter stack to object stack)
  38.   7 : OPUSH  OSTACK -2 OVER +!  @ !  ;   ( n ---  )
  39.   8
  40.   9 ( Pop object stack and discard)
  41.  10 : OPOP   2 OSTACK  +!  ;             ( --- )
  42.  11
  43.  12 ( Copy top of object stack and ADD to top 
  44.       of parameter stack)
  45.  13 : OCOP+  OSTACK @ @ +  ;      ( n --- n )
  46.  14
  47.  15                                       -->
  48.  
  49. Screen # 3
  50.   0 ( Type definitions                                    
  51.   1
  52.   2 ( Compile offset into instance variable and 
  53.       bump the total)
  54.   3 : OFFSET  SIZE @ 2 + , SIZE +! ;  ( size ---  )
  55.   4
  56.   5 ( Purely for brevity)
  57.   6 : COMPLIT  [COMPILE] LITERAL ;
  58.   7
  59.   8 ( Compile code to add offset into object body)
  60.   9 : COMPILE.ADDOFF   COMPLIT COMPILE OCOP+ ;
  61.  10
  62.  11 ( Create a new instance variable of 'size' bytes)
  63.  12 : VAR   CREATE  OFFSET              ( size ---  )
  64.  13                 IMMEDIATE
  65.  14         DOES>   @ COMPILE.ADDOFF ;
  66.  15                                 -->
  67.  
  68. Screen # 4
  69.   0 ( Type definitions                                    
  70.   1
  71.   2 ( Open a type declaration)
  72.   3 : TYPE>  LATEST PUBLIC !  ( NFA of last public word)
  73.   4          CREATE           ( Make a header)
  74.   5          HERE LASTLOCAL ! ( Store its PFA)
  75.   6          0 SIZE !         ( Initialisations)
  76.   7          TRUE  IN.TYPE.DEF? !
  77.   8          FALSE INHERIT ! ;
  78.   9
  79.  10 ( Mark boundary which hides the 
  80.       instance variables)
  81.  11 : OPS>   HERE            ( Address following 
  82.                                last VAR)
  83.  12          0 C,            ( Make dummy name field)
  84.  13          LATEST ,        ( Link field points to 
  85.                                last VAR)
  86.  14          DUP CONTEXT @ ! ( Let Forth know about 
  87.                                dummy word)
  88.  15          N>LINK OPS ! ;  ( Save its LFA)    -->
  89.  
  90. Screen # 5
  91.   0 ( Type definitions                                    
  92.   1
  93.   2 ( Save current vocabulary; set operations 
  94.       vocabulary)
  95.   3 : UNLOCK  CONTEXT @ DUP  @ STASH !  ! ; 
  96.       ( key ---  )
  97.   4
  98.   5 ( Restore current vocabulary)
  99.   6 : LOCK    STASH @ CONTEXT @ ! ;
  100.   7
  101.   8 ( Look up an operation in its type vocabulary)    
  102.     ( key --- CFA)
  103.   9 : FINDOP  BL WORD SWAP       ( Get operation name)
  104.  10           UNLOCK FIND LOCK   ( Find it)
  105.  11        0= ABORT" unrecognised operator " ; 
  106.             ( Abort if not found)
  107.  12
  108.  13
  109.  14
  110.  15                                             -->
  111.  
  112. Screen # 6
  113.   0 ( Type definitions                                    
  114.   1
  115.   2 ( Execute an operation immediately, if found)
  116.   3 : DO.OP   SWAP OPUSH FINDOP EXECUTE OPOP ;   
  117.      ( addr key --- ?)
  118.   4
  119.   5 ( Compile operation calling sequence)
  120.   6 : COMPILE.CALL  COMPILE OPUSH , COMPILE OPOP ; 
  121.      ( CFA ---  )
  122.   7
  123.   8 ( Look-up operation and compile it)
  124.   9 : COMPILE.OP    FINDOP SWAP COMPLIT  
  125.       ( addr key ---  )
  126.  10                 COMPILE.CALL ;
  127.  11
  128.  12 ( Fetch size field contents from instance 
  129.       variable or type)
  130.  13 : SZ@  2 + @  ;           ( addr --- size)
  131.  14
  132.  15 : SELF ; ( Optional; used for readability only) -->
  133.  
  134. Screen # 7
  135.   0 ( Type definitions                                    
  136.   1
  137.   2 ( Create an instance variable of a predefined type) 
  138.     ( addr --- )
  139.   3 : MAKE.STRUCTVAR  DUP SZ@   ( get size)
  140.   4                 SWAP @    ( Get key)
  141.   5                 CREATE , OFFSET ( Store key and size)
  142.   6                          IMMEDIATE
  143.   7       DOES> DUP @ SWAP SZ@ 2 - ( Get key and offset)
  144.   8             COMPILE.ADDOFF     ( Compile code....  )
  145.   9             FINDOP             ( to treat as..... )
  146.  10             COMPILE.CALL ;     ( an object. )
  147.  11
  148.  12 ( Compile or interpret an operation 
  149.       according to state)
  150.  13 : DO.OR.COMP   STATE @ IF   COMPILE.OP 
  151.       ( addr key ---  )
  152.  14                        ELSE DO.OP
  153.  15                        ENDIF ;                 -->
  154.  
  155. Screen # 8
  156.   0 ( Type definitions                                    
  157.   1
  158.   2 ( Allot space initialised to zeroes)
  159.   3 : ALLOTZ   DUP HERE SWAP 0 FILL ALLOT ; ( n ---  )
  160.   4
  161.   5 ( Execute an operation called INIT if 
  162.       there is one)
  163.   6 : INITIALIZE SWAP OPUSH
  164.   7        UNLOCK   LIT" INIT"  FIND LOCK
  165.   8        IF EXECUTE ELSE DROP ENDIF  OPOP ;
  166.   9
  167.  10 ( Create a new instance of a type)
  168.     ( addr ---  )
  169.  11 : MAKE.INSTANCE  CREATE HERE SWAP
  170.  12            DUP @ DUP ,      ( Store key into instc)
  171.  13            SWAP SZ@ ALLOTZ  ( Allot its storage)
  172.  14            INITIALIZE               IMMEDIATE
  173.  15            DOES> DUP @ DO.OR.COMP ;             -->
  174.  
  175. Screen # 9
  176.   0 ( Type definitions                                    
  177.   1
  178.   2 : INCLUDE>  ' >BODY @ INHERIT ! ;  
  179.      ( Inherit ops from old type)
  180.   3
  181.   4 ( Juggle dictionary pointers to seal the type body)
  182.   5 : LINKS  HERE BODY> >LINK  PUBLIC @ SWAP !  ( --- )
  183.   6          LASTLOCAL @ BODY> >NAME OPS @ !
  184.   7          INHERIT @ LASTLOCAL @ BODY> >LINK  ! ;
  185.   8
  186.   9 ( Close type declaration)
  187.  10 : ENDTYPE> LATEST CREATE LINKS  
  188.      ( Close the body)
  189.  11    , SIZE @ ,        ( Store key and size)
  190.  12      FALSE IN.TYPE.DEF? !
  191.  13      DOES>  IN.TYPE.DEF? @ IF   MAKE.STRUCTVAR
  192.  14                 ELSE MAKE.INSTANCE
  193.  15                 ENDIF ;              -->
  194.  
  195. Screen # 10
  196.   0 ( Array definitions                                  
  197.   1
  198.   2 ( Calculate address of array element)
  199.   3 : INDEX+  ROT * + ;  ( index PFA width ---  addr)
  200.   4
  201.   5 ( Interpret an array operation)    
  202.     ( index PFA key ---  )
  203.   6 : ARRAY.DO.OP  FINDOP               
  204.     ( Get operation CFA)
  205.   7                ROT ROT 4 + DUP @    
  206.                    ( Get width of element)
  207.   8                INDEX+
  208.                    ( Calculate element address)
  209.   9                OPUSH EXECUTE OPOP ; ( Do it!)
  210.  10
  211.  11 ( Place index on stack at compile time)
  212.  12 : VAL[   TRUE VAL ! [COMPILE] [ ;    IMMEDIATE
  213.  13
  214.  14 ( Reset the VAL flag)
  215.  15 : ~VAL   FALSE VAL ! ;                    -->
  216.  
  217. Screen # 11
  218.   0 ( Array definitions                                  
  219.   1
  220.   2 ( Compile an array operation) 
  221.     ( {index} PFA key ---  )
  222.   3 : ARRAY.COMP.OP  FINDOP >R     
  223.         ( Get op CFA and stash it)
  224.   4                  4 + DUP @           
  225.                      ( Get width of array)
  226.   5                  VAL @                
  227.                      ( Index at compile time?)
  228.   6                  IF   INDEX+ COMPLIT         
  229.                      ( Compile el. addr)
  230.   7                  ELSE SWAP COMPLIT COMPLIT   
  231.                      ( or code to calc )
  232.   8                  COMPILE INDEX+  ( at runtime)
  233.   9                  ENDIF
  234.  10                  R> COMPILE.CALL  ~VAL ; 
  235.                      ( compile op call)
  236.  11
  237.  12 ( Compile or interpret an array op)     
  238.     ( index PFA key ---  )
  239.  13 : ARRAY.DO.OR.COMP  STATE @ IF   ARRAY.COMP.OP
  240.  14                             ELSE ARRAY.DO.OP
  241.  15                             ENDIF ;         -->
  242.  
  243. Screen # 12
  244.   0 ( Array definitions                                  
  245.   1 ( Create a typed array as an instance variable)
  246.     ( count PFA --- )
  247.   2 : ARRAY.VAR  CREATE DUP @ , OVER ,  
  248.      ( Store key and count)
  249.   3       SZ@ DUP ,        ( Store width of element)
  250.   4       *                ( Size = count * width)
  251.   5       OFFSET           ( Store offset etc.)
  252.   6       IMMEDIATE
  253.   7       DOES>  DUP @            ( Get key)
  254.   8       FINDOP >R        ( Get op CFA and stash it)
  255.   9       DUP 6 + @                      ( Get offset)
  256.  10       2 - SWAP 4 + @                 ( Get width)
  257.  11 ( Compile el addr)  VAL @ IF   INDEX+ COMPILE.ADDOFF
  258.  12 ( or code to...)          ELSE SWAP COMPLIT COMPLIT
  259.  13 ( calculate it...)             COMPILE INDEX+
  260.  14 ( at runtime)                  COMPILE OCOP+
  261.  15 ENDIF R> COMPILE.CALL ~VAL ;       -->
  262.  
  263. Screen # 13
  264.   0 ( Array definitions                                  
  265.   1
  266.   2 ( Make a new array instance)    ( count PFA ---  )
  267.   3 : MAKE.ARRAY   CREATE 2DUP @ , ,  
  268.                    ( Store key and count)
  269.   4                       SZ@ DUP , SWAP     
  270.                           ( Store width)
  271.   5                       *  ALLOTZ          
  272.                           ( Allot the space)
  273.   6                       IMMEDIATE
  274.   7                DOES> DUP @ ARRAY.DO.OR.COMP ;
  275.   8
  276.   9 ( Create an array object or variable)   
  277.     ( count +++   )
  278.   10 : ARRAY-OF     ' >BODY
  279.   11                IN.TYPE.DEF? @
  280.   12                IF  ARRAY.VAR
  281.   13                ELSE MAKE.ARRAY
  282.   14                ENDIF ;
  283.   15
  284.  
  285. Screen # 14
  286.   0 (    DATA STRUCTURES EMPLOYED INTERNALLY
  287.   1
  288.   2 OBJECT   <--------------- width ------------------>
  289.   3   +--------+-----+--------------------------------+
  290.   4   | header | key |       storage fields           |
  291.   5   +--------+-----+--------------------------------+
  292.   6
  293.   7 ARRAY-OF OBJECTS
  294.   8   +--------+-----+-------+-------+----------------+
  295.   9   | header | key | count | width |   elements     |
  296.  10   +--------+-----+-------+-------+----------------+
  297.  11
  298.  12 TYPE DEFINING WORD
  299.  13   +--------+-----+------+
  300.  14   | header | key | size |
  301.  15   +--------+-----+------+                         )
  302.  
  303. Screen # 15
  304.   0 (
  305.   1 VAR NAME
  306.   2   +--------+--------+
  307.   3   | header | offset |
  308.   4   +--------+--------+
  309.   5
  310.   6 STRUCTVAR NAME
  311.   7   +--------+-----+--------+
  312.   8   | header | key | offset |
  313.   9   +--------+-----+--------+
  314.  10
  315.  11 ARRAY.VAR NAME
  316.  12   +--------+-----+-------+-------+--------+
  317.  13   | header | key | count | width | offset |
  318.  14   +--------+-----+-------+-------+--------+
  319.  15                                              )